package com.fg.cloud.system.controller.product;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.fg.cloud.common.PageUtils;
import com.fg.cloud.common.Result;
import com.fg.cloud.common.ServiceException;
import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.construction.Select;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.manager.entry.product.ProductInfoEntity;
import com.fg.cloud.manager.entry.product.ProductSkuEntity;
import com.fg.cloud.manager.service.product.ProductInfoService;
import com.fg.cloud.manager.service.system.SystemProductService;
import com.fg.cloud.system.controller.BaseSystemController;

@Controller
@RequestMapping("SystemProduct")
public class SystemProductController extends BaseSystemController{

	@Autowired
	private SystemProductService systemProductService; 

	@Autowired
	private ProductInfoService productService ; 
	
	/**
	 * Description:跳转到商品页面
	 * @param request
	 * @return
	 * @author dingjiang
	 * @date 2017年10月23日下午5:27:18
	 */
	@RequestMapping(value="/index")
	public String index(HttpServletRequest request) {
		return "product/product";
	}
	
	/**
	 * 查询全部产品
	 * */
	@RequestMapping(value="/findBySenior",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public Map<String ,Object> findBySenior(HttpServletRequest req , HttpServletResponse resp,
			String keyword,Long facId,
			Integer page, Integer rows, String sort, String order){
		try{
			if("isDefault".equals(sort)){
				sort = "default";
			}
			PageUtils<ProductSkuEntity> pageinfo = new PageUtils<ProductSkuEntity>(page, rows, sort, order);
			Map<String,Object> conditions = new HashMap<>();
			conditions.put("keyword", keyword);
			pageinfo.setCondition(conditions);
			pageinfo.setFacId(facId);
			systemProductService.findBySenior(pageinfo);
			Map<String ,Object> usmap = new HashMap<String ,Object>();
			usmap.put("total", pageinfo.getTotal());
			usmap.put("rows", pageinfo.getData());
			return usmap;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	  * Description:商品信息修改
	  * @param req
	  * @param resp
	  * @param product 商品信息
	  * @author liushengchao
	  * @date 2017年8月3日下午6:30:31
	 */
	@RequestMapping(value="/modify",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void modify(HttpServletRequest req , HttpServletResponse resp,Long facId,
			String product, String skuListStr, String paramListStr){
		try{
			if(facId == null || facId < 0){
				doResult(resp, false, StatusCode.LACKVALUE,null,"facId必填");
			}
			product = StringEscapeUtils.unescapeHtml4(product);
			ProductInfoEntity entity = JSON.parseObject(product, ProductInfoEntity.class);
			Result result = productService.modifyProduct(entity, facId);
			doResult(resp, result.isSuccess(), result.getCode(),result.getData(),result.getMessage());
		}catch(ServiceException e){
			doResult(resp, true, StatusCode.SAVEERROR, null, ResultType.ERROE);
		}
		
	}
	
	/**
	 * 删除商品
	 * */
	@RequestMapping(value="/deleteProdect",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public Long deleteProdect(HttpServletRequest req , HttpServletResponse resp ,String ids){
		List<Long> id = toLong(ids);
		Long flag = systemProductService.delete(id);
		return flag;
	}
	
	public List<Long> toLong(String ids){
		String[] id1 = ids.split(",");
		List<Long> id = new ArrayList<Long>();
		for(int i = 0;i<id1.length;i++){
			if(!"".equals(id1[i])){
				id.add(Long.valueOf(id1[i]));
			}
		}
		
		return id;
	}
	
	/**
	 * 查询所有厂商
	 * */
	@RequestMapping(value="/findAllFactory",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public List<Select> findAllFactory(HttpServletRequest req , HttpServletResponse resp){
		return systemProductService.findAllFactory();
	}
}
